<template>
  <div class="comment-item">
    <div class="comment-header">
      <div class="comment-author">
        <el-avatar :size="32">{{ comment.authorName.charAt(0) }}</el-avatar>
        <div class="author-info">
          <div class="author-name">{{ comment.authorName }}</div>
          <div class="comment-time">{{ formatTime(comment.createTime) }}</div>
        </div>
      </div>
      <div class="comment-actions">
        <el-button size="small" @click="showReply = !showReply">回复</el-button>
        <el-button 
          v-if="String(comment.authorId) === userInfo.id || String(postAuthorId) === userInfo.id"
          size="small" 
          type="danger" 
          @click="deleteComment(comment)"
        >删除</el-button>
      </div>
    </div>
    <div class="comment-content">{{ comment.content }}</div>
    <div class="comment-footer">
      <el-button 
        size="small" 
        :type="comment.isLiked ? 'primary' : 'default'"
        @click="toggleCommentLike(comment)"
      >
        <el-icon><ThumbsUp /></el-icon>
        {{ comment.likeCount }} 点赞
      </el-button>
    </div>
    <!-- 回复输入框 -->
    <div v-if="showReply" class="reply-input">
      <el-input
        v-model="replyContent"
        type="textarea"
        :rows="2"
        placeholder="回复内容"
        maxlength="500"
        show-word-limit
      />
      <el-button type="primary" size="small" @click="submitReply" :loading="replying" style="margin-top:8px;">发表回复</el-button>
      <el-button size="small" @click="showReply = false" style="margin-top:8px;">取消</el-button>
    </div>
    <!-- 子评论递归 -->
    <div class="child-comments" v-if="comment.children && comment.children.length">
      <CommentItem
        v-for="child in comment.children"
        :key="child.id"
        :comment="child"
        :userInfo="userInfo"
        :postAuthorId="postAuthorId"
        :formatTime="formatTime"
        :deleteComment="deleteComment"
        :toggleCommentLike="toggleCommentLike"
        :addComment="addComment"
      />
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { ThumbsUp } from '@element-plus/icons-vue'

export default {
  name: 'CommentItem',
  components: { ThumbsUp, CommentItem: undefined }, // 递归注册
  props: {
    comment: Object,
    userInfo: Object,
    postAuthorId: [String, Number],
    formatTime: Function,
    deleteComment: Function,
    toggleCommentLike: Function,
    addComment: Function
  },
  setup(props) {
    const showReply = ref(false)
    const replyContent = ref('')
    const replying = ref(false)

    const submitReply = async () => {
      if (!replyContent.value.trim()) return
      replying.value = true
      await props.addComment(replyContent.value, props.comment.id)
      replyContent.value = ''
      showReply.value = false
      replying.value = false
    }

    return {
      showReply,
      replyContent,
      replying,
      submitReply
    }
  }
}
</script>

<style scoped>
.child-comments {
  margin-left: 40px;
  border-left: 2px solid #e0e0e0;
  padding-left: 16px;
  margin-top: 10px;
}

.comment-item {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.03);
  padding: 16px 16px 12px 16px;
  margin-bottom: 18px;
  border: 1px solid #f0f0f0;
  transition: box-shadow 0.2s;
}
.comment-item:hover {
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}
.comment-author {
  display: flex;
  align-items: center;
  gap: 10px;
}
.author-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.author-name {
  font-weight: 500;
  color: var(--el-text-color-primary);
  font-size: 15px;
}
.comment-time {
  color: #aaa;
  font-size: 12px;
}
.comment-actions {
  display: flex;
  gap: 6px;
}
.comment-actions .el-button {
  padding: 2px 10px;
  font-size: 12px;
  border-radius: 6px;
}

.comment-content {
  color: var(--el-text-color-regular);
  line-height: 1.6;
  margin-bottom: 8px;
  font-size: 15px;
  word-break: break-all;
}

.comment-footer {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 2px;
}
.comment-footer .el-button {
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 6px;
}

.reply-input {
  background: #f7f8fa;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.04);
  padding: 12px 12px 8px 12px;
  margin: 10px 0 8px 0;
  border: 1px solid #e0e0e0;
}
.reply-input .el-input {
  margin-bottom: 6px;
}
.reply-input .el-button {
  margin-right: 8px;
}

/* 分隔线 */
.comment-item:not(:last-child) {
  border-bottom: 1px solid #f0f0f0;
}
</style> 